load MAT/Step_2_firm_data_alpha4;
N = size(ISO,2);
eval([strcat('load MAT/rho',rhoT,'eta',etaT,'lambda',lambdaT,'_psi',num2str(varphi),'_sigma',num2str(sigmaT),'/Step_5_output_alpha',num2str(alphaT),...
         '_rho',rhoT,'_',shockT,'_approx.mat')]); 
eta = .1;
%RGDP_hat_i = RGD_doubledef_hat_n;
%VA_i_0 = sum(reshape(VA_mj_0,32,40),1)';
VA_f_0 = VA_fj_0;
%RGDP_hat_f = RGDP_doubledef_hat_fj;
RGDP_hat_FR_prod = RGDP_doubledef_hat_n(15)-1;
VA_iw_0 = sum(reshape(VA_mj_0,32,40),1)';
VA_iw_0(france)=0;
sh_iw=VA_iw_0/sum(VA_iw_0);
RGDP_hat_W_prod = sh_iw'*(RGDP_doubledef_hat_n-1);
lnva_grW_prod = log(VA_f_0)*RGDP_hat_W_prod;
lnva_grFR_prod = log(VA_f_0)*RGDP_hat_FR_prod;
e_f_prod = (RGDP_doubledef_hat_fj-1)/eta;

B_Wprod10_ols_a = regstats(RGDP_doubledef_hat_fj-1,lnva_grW_prod,'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wprod10_fe_a = regstats(RGDP_doubledef_hat_fj-1,[firmsecD_sorted(:,1:end-1) lnva_grW_prod],'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wprod10_ols_b = regstats(e_f_prod,log(VA_f_0),'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wprod10_fe_b = regstats(e_f_prod,[firmsecD_sorted(:,1:end-1) log(VA_f_0)],'linear',{'covb','tstat','rsquare','adjrsquare'});

shockT='WORLDpref_p10';
eval([strcat('load MAT/rho',rhoT,'eta',etaT,'lambda',lambdaT,'_psi',num2str(varphi),'_sigma',num2str(sigmaT),'/Step_5_output_alpha',num2str(alphaT),...
         '_rho',rhoT,'_',shockT,'_approx.mat')]); 
VA_f_0 = VA_fj_0;
RGDP_hat_FR_pref = RGDP_doubledef_hat_n(15)-1;
VA_iw_0 = sum(reshape(VA_mj_0,32,40),1)';
VA_iw_0(france)=0;
sh_iw=VA_iw_0/sum(VA_iw_0);
RGDP_hat_W_pref = sh_iw'*(RGDP_doubledef_hat_n-1);
lnva_grW_pref = log(VA_f_0)*RGDP_hat_W_pref;
lnva_grFR_pref = log(VA_f_0)*RGDP_hat_FR_pref;
e_f_pref = (RGDP_doubledef_hat_fj-1)/eta;

B_Wpref10_ols_a = regstats(RGDP_doubledef_hat_fj-1,lnva_grW_pref,'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wpref10_fe_a = regstats(RGDP_doubledef_hat_fj-1,[firmsecD_sorted(:,1:end-1) lnva_grW_pref],'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wpref10_ols_b = regstats(e_f_pref,log(VA_f_0),'linear',{'covb','tstat','rsquare','adjrsquare'});
B_Wpref10_fe_b = regstats(e_f_pref,[firmsecD_sorted(:,1:end-1) log(VA_f_0)],'linear',{'covb','tstat','rsquare','adjrsquare'});

tab_Wprod10_ols_coef_a = [B_Wprod10_ols_a.tstat.beta(1);B_Wprod10_ols_a.tstat.se(1);B_Wprod10_ols_a.tstat.beta(end);B_Wprod10_ols_a.tstat.se(end)];
tab_Wprod10_ols_R2_a = [B_Wprod10_ols_a.adjrsquare];
tab_Wprod10_ols_obs_a = [B_Wprod10_ols_a.tstat.dfe+size(B_Wprod10_ols_a.tstat.beta,1)];

tab_Wpref10_ols_coef_a = [B_Wpref10_ols_a.tstat.beta(1);B_Wpref10_ols_a.tstat.se(1);B_Wpref10_ols_a.tstat.beta(end);B_Wpref10_ols_a.tstat.se(end)];
tab_Wpref10_ols_R2_a = [B_Wpref10_ols_a.adjrsquare];
tab_Wpref10_ols_obs_a = [B_Wpref10_ols_a.tstat.dfe+size(B_Wpref10_ols_a.tstat.beta,1)];

tab_Wprod10_fe_coef_a = [B_Wprod10_fe_a.tstat.beta(1);B_Wprod10_fe_a.tstat.se(1);B_Wprod10_fe_a.tstat.beta(end);B_Wprod10_fe_a.tstat.se(end)];
tab_Wprod10_fe_R2_a = [B_Wprod10_fe_a.adjrsquare];
tab_Wprod10_fe_obs_a = [B_Wprod10_fe_a.tstat.dfe+size(B_Wprod10_fe_a.tstat.beta,1)];

tab_Wpref10_fe_coef_a = [B_Wpref10_fe_a.tstat.beta(1);B_Wpref10_fe_a.tstat.se(1);B_Wpref10_fe_a.tstat.beta(end);B_Wpref10_fe_a.tstat.se(end)];
tab_Wpref10_fe_R2_a = [B_Wpref10_fe_a.adjrsquare];
tab_Wpref10_fe_obs_a = [B_Wpref10_fe_a.tstat.dfe+size(B_Wpref10_fe_a.tstat.beta,1)];

tab_Wprod10_ols_coef_b = [B_Wprod10_ols_b.tstat.beta(1);B_Wprod10_ols_b.tstat.se(1);B_Wprod10_ols_b.tstat.beta(end);B_Wprod10_ols_b.tstat.se(end)];
tab_Wprod10_ols_R2_b = [B_Wprod10_ols_b.adjrsquare];
tab_Wprod10_ols_obs_b = [B_Wprod10_ols_b.tstat.dfe+size(B_Wprod10_ols_b.tstat.beta,1)];

tab_Wpref10_ols_coef_b = [B_Wpref10_ols_b.tstat.beta(1);B_Wpref10_ols_b.tstat.se(1);B_Wpref10_ols_b.tstat.beta(end);B_Wpref10_ols_b.tstat.se(end)];
tab_Wpref10_ols_R2_b = [B_Wpref10_ols_b.adjrsquare];
tab_Wpref10_ols_obs_b = [B_Wpref10_ols_b.tstat.dfe+size(B_Wpref10_ols_b.tstat.beta,1)];

tab_Wprod10_fe_coef_b = [B_Wprod10_fe_b.tstat.beta(1);B_Wprod10_fe_b.tstat.se(1);B_Wprod10_fe_b.tstat.beta(end);B_Wprod10_fe_b.tstat.se(end)];
tab_Wprod10_fe_R2_b = [B_Wprod10_fe_b.adjrsquare];
tab_Wprod10_fe_obs_b = [B_Wprod10_fe_b.tstat.dfe+size(B_Wprod10_fe_b.tstat.beta,1)];

tab_Wpref10_fe_coef_b = [B_Wpref10_fe_b.tstat.beta(1);B_Wpref10_fe_b.tstat.se(1);B_Wpref10_fe_b.tstat.beta(end);B_Wpref10_fe_b.tstat.se(end)];
tab_Wpref10_fe_R2_b = [B_Wpref10_fe_b.adjrsquare];
tab_Wpref10_fe_obs_b = [B_Wpref10_fe_b.tstat.dfe+size(B_Wpref10_fe_b.tstat.beta,1)];

tab_all_ols_a = table([tab_Wprod10_ols_coef_a;tab_Wprod10_ols_obs_a;tab_Wprod10_ols_R2_a],...
    [tab_Wpref10_ols_coef_a;tab_Wpref10_ols_obs_a;tab_Wpref10_ols_R2_a]);
tab_all_ols_a.Properties.VariableNames = {'WorldProdShock' 'WorldPrefShock'};
tab_all_ols_a.Properties.RowNames = {'Constant' 'se1' 'log(VA_0)*RGDPgr_W' 'se2' 'obs' 'adj-R2'};

tab_all_fe_a = table([tab_Wprod10_fe_coef_a;tab_Wprod10_fe_obs_a;tab_Wprod10_fe_R2_a],...
    [tab_Wpref10_fe_coef_a;tab_Wpref10_fe_obs_a;tab_Wpref10_fe_R2_a]);
tab_all_fe_a.Properties.VariableNames = {'WorldProdShock' 'WorldPrefShock'};
tab_all_fe_a.Properties.RowNames = {'Constant' 'se1' 'log(VA_0)*RGDPgr_W' 'se2' 'obs' 'adj-R2'};
writetable(tab_all_ols_a,'Tables/motivation_model_regs_CES.xlsx','Sheet','OLS_inter','WriteRowNames',true);
writetable(tab_all_fe_a,'Tables/motivation_model_regs_CES.xlsx','Sheet','FE_inter','WriteRowNames',true);

tab_all_ols_b = table([tab_Wprod10_ols_coef_b;tab_Wprod10_ols_obs_b;tab_Wprod10_ols_R2_b],...
    [tab_Wpref10_ols_coef_b;tab_Wpref10_ols_obs_b;tab_Wpref10_ols_R2_b]);
tab_all_ols_b.Properties.VariableNames = {'WorldProdShock' 'WorldPrefShock'};
tab_all_ols_b.Properties.RowNames = {'Constant' 'se1' 'log(VA_0)*RGDPgr_W' 'se2' 'obs' 'adj-R2'};

tab_all_fe_b = table([tab_Wprod10_fe_coef_b;tab_Wprod10_fe_obs_b;tab_Wprod10_fe_R2_b],...
    [tab_Wpref10_fe_coef_b;tab_Wpref10_fe_obs_b;tab_Wpref10_fe_R2_b]);
tab_all_fe_b.Properties.VariableNames = {'WorldProdShock' 'WorldPrefShock'};
tab_all_fe_b.Properties.RowNames = {'Constant' 'se1' 'log(VA_0)*RGDPgr_W' 'se2' 'obs' 'adj-R2'};
writetable(tab_all_ols_b,'Tables/motivation_model_regs_CES.xlsx','Sheet','OLS_size','WriteRowNames',true);
writetable(tab_all_fe_b,'Tables/motivation_model_regs_CES.xlsx','Sheet','FE_size','WriteRowNames',true);
